package lili.com.my.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import lili.com.my.R;

public class LoginActivity extends AppCompatActivity {

    private EditText musername;
    private EditText mpassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        musername = (EditText) findViewById(R.id.et_user_name);
        mpassword = (EditText) findViewById(R.id.et_psw);
    }
    public void back(View view){
        finish();
    }
    public void login(View view){
        RequestParams params = new RequestParams("http://1633.applinzi.com/public/?s=App.User.Login");

        params.addQueryStringParameter("username", musername.getText().toString());
        params.addQueryStringParameter("password", mpassword.getText().toString());
        x.http().get(params, new Callback.CommonCallback<String>() {

            public void onSuccess(String result) {
                Gson gson=new Gson();
                LoginModel loginModel=gson.fromJson(result,LoginModel.class);
                if (loginModel.code==200){
                    SharedPreferences sp=getSharedPreferences("login",MODE_PRIVATE);
                    SharedPreferences.Editor editor=sp.edit();
                    editor.putBoolean("islogin",true);
                    editor.commit();
                    finish();
                }else {
                    Toast.makeText(LoginActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
                }
            }

            public void onError(Throwable ex, boolean isOnCallback) {

            }
            public void onCancelled(CancelledException cex) {

            }

            public void onFinished() {

            }
        });
    }
    public void register(View view){
        Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
        startActivity(intent);
    }
    }

